package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Subjects;
import com.xmy.cultivate.entity.TrainPromoteShutnum;
import com.xmy.cultivate.entity.TrainPromoteShutnumDetail;
import com.xmy.cultivate.service.ITrainPromoteShutnumDetailService;
import com.xmy.cultivate.service.ITrainPromoteShutnumService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 教师晋升关数设置 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-10-18
 */
@RestController
@RequestMapping("/api/trainPromoteShutnum")
public class TrainPromoteShutnumController extends BaseController{

    @Autowired
    ITrainPromoteShutnumService iTrainPromoteShutnumService;
    @Autowired
    ITrainPromoteShutnumDetailService iTrainPromoteShutnumDetailService;



    /**
     * 获取教师晋升列表
     * @param pageNum
     * @param pageSize
     * @param teacherLevel
     * @param num
     * @return
     */
    @GetMapping("/trainPromoteShutnumList")
    public Result trainPromoteShutnumList(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize,
                          @RequestParam(value = "teacherLevel",required = false) String teacherLevel,
                          @RequestParam(value = "num",required = false) String num
    ){
        IPage<TrainPromoteShutnum> iPage = new Page<>(pageNum,pageSize);
        iTrainPromoteShutnumService.findAll(iPage,teacherLevel,num);
        return new Result(Code.OK,new PageResult(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
    }

    /**
     * 添加教师晋升关数设置
     * @param trainPromoteShutnum
     * @return
     */
    @PostMapping("/addTrainPromoteShutnum")
    public Result addTrainPromoteShutnum(@RequestBody TrainPromoteShutnum trainPromoteShutnum){
        QueryWrapper<TrainPromoteShutnum> qw = new QueryWrapper<>();
        qw.eq("teacher_level",trainPromoteShutnum.getTeacherLevel());
        qw.eq("num",trainPromoteShutnum.getNum());
        qw.eq("deleted",0);
        long count = iTrainPromoteShutnumService.count(qw);
        if(count>0){
            return new Result(Code.SAVE_ERROR,null,"该教师等级该关数已存在");
        }
        trainPromoteShutnum.setDeleted(0);
        boolean save = iTrainPromoteShutnumService.save(trainPromoteShutnum);
        if(save){
            return new Result(Code.OK,null,"添加成功");
        }
        return new Result(Code.ERROR,null,"添加失败");
    }

    /**
     * 修改教师晋升关数设置
     * @param trainPromoteShutnum
     * @return
     */
    @PutMapping("/updateTrainPromoteShutnum")
    public Result updateTrainPromoteShutnum(@RequestBody TrainPromoteShutnum trainPromoteShutnum){
        TrainPromoteShutnum trainPromoteShutnumOld = iTrainPromoteShutnumService.getById(trainPromoteShutnum.getId());
        if (Objects.isNull(trainPromoteShutnumOld)){
            return new Result(Code.SAVE_ERROR,null,"该数据不存在");
        }
        QueryWrapper<TrainPromoteShutnum> qw = new QueryWrapper<>();
        qw.ne("id",trainPromoteShutnum.getId());
        qw.eq("teacher_level",trainPromoteShutnum.getTeacherLevel());
        qw.eq("num",trainPromoteShutnum.getNum());
        qw.eq("deleted",0);
        long count = iTrainPromoteShutnumService.count(qw);
        if(count>0){
            return new Result(Code.SAVE_ERROR,null,"该教师等级该关数已存在");
        }
        boolean update = iTrainPromoteShutnumService.updateById(trainPromoteShutnum);
        if(update){
            return new Result(Code.OK,null,"修改成功");
        }
        return new Result(Code.ERROR,null,"修改失败");
    }





    /**
     * 删除教师晋升关数设置 单个删除 或者 批量删除
     * @param reMap
     * @return
     */
    @DeleteMapping("/deleteTrainPromoteShutnum")
    public Result deleteTrainPromoteShutnum(@RequestBody Map reMap) {

        List<String> ids = (List<String>) reMap.get("ids");
        if (ids == null || ids.isEmpty()) {
            return new Result(Code.DELETE_ERROR, "", "请选择要删除的数据");
        }
        QueryWrapper<TrainPromoteShutnumDetail> qw = new QueryWrapper<>();
        qw.in("shutnum_id", ids);
        long count = iTrainPromoteShutnumDetailService.count(qw);
        if (count > 0){
            return new Result(Code.DELETE_ERROR, null, "请先删除关数中设置的类型");
        }
        QueryWrapper<TrainPromoteShutnum> promoteShutnumQueryWrapper = new QueryWrapper<>();
        promoteShutnumQueryWrapper.in("id", ids);
        boolean b = iTrainPromoteShutnumService.remove(promoteShutnumQueryWrapper);
        if (b) {
            return new Result(Code.DELETE_OK, "", Code.DELETE_OK_MSG);
        }
        return new Result(Code.DELETE_ERROR, "", Code.DELETE_ERROR_MSG);
    }

}
